/***********************************************************
constants
***********************************************************/

// reference to the interface defined in pcILogger.idl
const pcILogger = Components.interfaces.pcILogger;

// reference to the required base interface that all components must support
const nsISupports = Components.interfaces.nsISupports;

// UUID uniquely identifying our component
// You can get from: http://kruithof.xs4all.nl/uuid/uuidgen here
const CLASS_ID = Components.ID("{73e886c0-4a1c-11db-b0de-0800200c9a66}");

// description
const CLASS_NAME = "This xpcom takes care of logging messages into a file";

// textual unique identifier
const CONTRACT_ID = "@peercafe.org/pcLogger;1";

/***********************************************************
class definition
***********************************************************/

//class constructor
function pcLogger() {
};

// class definition
pcLogger.prototype = {
  pathToLogFile: "pcLog.txt",

  /* Log the given message on disk in home/pcLog.txt */
  pcLog: function(message){
	try{
		// creating the file object
		var file = Components.classes["@mozilla.org/file/directory_service;1"]
		                     .getService(Components.interfaces.nsIProperties)
		                     .get("Home", Components.interfaces.nsIFile);

		file.append(this.pathToLogFile);

		// file is nsIFile, data is a string
		var foStream = Components.classes["@mozilla.org/network/file-output-stream;1"]
		                         .createInstance(Components.interfaces.nsIFileOutputStream);

		foStream.init(file, 0x02 | 0x10 | 0x08, 0664, 0); // write, append, create
		message = new Date().toString() + ' : ' + message + '\n'
		foStream.write(message, message.length);
		foStream.close();
	}catch(e){
		// debug string
	}
  },

  QueryInterface: function(aIID){
    if (!aIID.equals(pcILogger) &&    
        !aIID.equals(nsISupports))
      throw Components.results.NS_ERROR_NO_INTERFACE;
    return this;
  }
};

/***********************************************************
class factory

This object is a member of the global-scope Components.classes.
It is keyed off of the contract ID. Eg:

mypcLogger= Components.classes["@dietrich.ganx4.com/pcLogger;1"].
                          createInstance(pcILogger);

***********************************************************/
var pcLoggerFactory = {
  createInstance: function (aOuter, aIID)
  {
    if (aOuter != null)
      throw Components.results.NS_ERROR_NO_AGGREGATION;
    return (new pcLogger()).QueryInterface(aIID);
  }
};

/***********************************************************
module definition (xpcom registration)
***********************************************************/
var pcLoggerModule = {
  _firstTime: true,
  registerSelf: function(aCompMgr, aFileSpec, aLocation, aType)
  {
    aCompMgr = aCompMgr.
        QueryInterface(Components.interfaces.nsIComponentRegistrar);
    aCompMgr.registerFactoryLocation(CLASS_ID, CLASS_NAME, 
        CONTRACT_ID, aFileSpec, aLocation, aType);
  },

  unregisterSelf: function(aCompMgr, aLocation, aType)
  {
    aCompMgr = aCompMgr.
        QueryInterface(Components.interfaces.nsIComponentRegistrar);
    aCompMgr.unregisterFactoryLocation(CLASS_ID, aLocation);        
  },
  
  getClassObject: function(aCompMgr, aCID, aIID)
  {
    if (!aIID.equals(Components.interfaces.nsIFactory))
      throw Components.results.NS_ERROR_NOT_IMPLEMENTED;

    if (aCID.equals(CLASS_ID))
      return pcLoggerFactory;

    throw Components.results.NS_ERROR_NO_INTERFACE;
  },

  canUnload: function(aCompMgr) { return true; }
};

/***********************************************************
module initialization

When the application registers the component, this function
is called.
***********************************************************/
function NSGetModule(aCompMgr, aFileSpec) { return pcLoggerModule; }
